﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OhMyGol.UI.Models.Grid
{
    public class GridPage<R> 
    {
        private int pagesCount;
        private int pageIndex;
        private int rowsCount;
        private IEnumerable<R> rows;
        private Func<R, string[]> toArray;
        private Func<R, int> getId;

        public GridPage(
            int pagesCount, 
            int pageIndex, 
            int rowsCount, 
            IEnumerable<R> rows, 
            Func<R, string[]> toArray, 
            Func<R, int> getId)
        {
            this.pagesCount = pagesCount;
            this.pageIndex = pageIndex;
            this.rowsCount = rowsCount;
            this.rows = rows;
            this.toArray = toArray;
            this.getId = getId;
        }

        public object ToObject()
        {
            return new
            {
                    total = this.pagesCount,
                    page = this.pageIndex,
                    records = this.rowsCount,
                    rows = this.GetGridRows()
            };
        }

        private object GetGridRows()
        {
            return this.rows.Select(row => new
            {
                i = this.getId(row),
                cell = this.toArray(row)
            });
        }
    }
}